﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using DataControl;
using System.Data;
using System.Collections;
using Com.Alipay;
using ATW.MT.BUS;
using BUS;
using System.Configuration;

namespace ATW.MT.CommonClass
{
    /// <summary>
    /// 合同生成类
    /// </summary>
    public class Contact
    {
        DataControl.DataAccess da = new DataControl.DataAccess();
        CommonHelp CH = new CommonHelp();
        //private string CusContactFilePath = "E:/Projects/安特威/AnTeWei/CommonFile/ContactDemo/Customer/CustomerContact.html";
        //private string SupContactFilePath = "E:/Projects/安特威/AnTeWei/CommonFile/ContactDemo/Supply/SupplyContact.html";
        /// <summary>
        /// 生成客户合同
        /// </summary>
        /// <param name="RequestID">询价表头ID</param>
        public string GenerateCustomerContact(string strContactId, string RequestID, string CusContactFilePath)
        {
            /*
             * 表头参数列表：
             * 【客户单位名称】
             * 【总价】
             * 【合同编号】
             * 
             * 表体参数列表：
             * 第一层：
             * 【序号】
             * 【产品名称】
             * 【阀体】
             * 【阀芯】
             * 【阀座】
             * 【阀杆】
             * 【填料】
             * 【温度】
             * 【压力等级】
             * 【密封等级】
             * 【设计标准】
             * 【结构标准】
             * 【连接方式】
             * 【法兰标准】
             * 
             * 第二层：
             * 【子序号】
             * 【口径】
             * 【数量】
             * 【单价】
             * 【总金额】
             */
            string CusName = "";
            decimal ToTalPrice = 0;
            ArrayList al = new ArrayList();
            try
            {
                //抓取内容数据
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat(@"select au.tauName from T_Request R 
                                    left join T_AppUser au on au.tauLoginId=R.tauloginid
                                    where R.RequestID='{0}'", RequestID);
                DataTable dtHead = da.ExecuteDataTable(sql.ToString());
                if (dtHead != null && dtHead.Rows.Count > 0)
                {
                    CusName = dtHead.Rows[0]["tauName"].ToString();
                }
                DataTable dtList = new DataTable();
                GetReqData(RequestID, ref dtHead, ref dtList);
                string seqno = CH.GetSequence("1");//合同号
                //读取、拼装合同
                StreamReader sr = new StreamReader(CusContactFilePath, Encoding.UTF8);
                string StrContact = sr.ReadToEnd();
                StrContact = StrContact.Replace("【客户单位名称】", CusName);
                StrContact = StrContact.Replace("【合同编号】", seqno);
                int _startIndex = StrContact.IndexOf("<!--产品头部开始-->") + 13;
                int _endIndex = StrContact.IndexOf("<!--产品头部结束-->");
                string repeater = StrContact.Substring(_startIndex, _endIndex - _startIndex);
                _startIndex = repeater.IndexOf("<!--子项开始-->") + 11;
                _endIndex = repeater.IndexOf("<!--子项结束-->");
                string subRepeater = repeater.Substring(_startIndex, _endIndex - _startIndex);
                string finalContent = "";
                if (dtHead.Rows.Count > 0)
                {
                    for (int i = 0; i < dtHead.Rows.Count; i++)
                    {
                        string _repeater = repeater;
                        string _subContent = "";
                        string Oid = dtHead.Rows[i]["Oid"].ToString();
                        _repeater = _repeater.Replace("【序号】", (i + 1).ToString());
                        _repeater = _repeater.Replace("【产品名称】", dtHead.Rows[i]["ProductionDesc"].ToString());
                        _repeater = _repeater.Replace("【阀体】", "【阀体】" + dtHead.Rows[i]["MID_BDesc"].ToString());
                        _repeater = _repeater.Replace("【阀芯】", "【阀芯】" + dtHead.Rows[i]["MID_IDesc"].ToString());
                        _repeater = _repeater.Replace("【阀座】", "【阀座】" + dtHead.Rows[i]["args02Desc"].ToString());
                        _repeater = _repeater.Replace("【阀杆】", "【阀杆】" + dtHead.Rows[i]["MID_SDesc"].ToString());
                        _repeater = _repeater.Replace("【填料】", "【填料】" + dtHead.Rows[i]["args09Desc"].ToString());
                        _repeater = _repeater.Replace("【温度】", "【温度】" + dtHead.Rows[i]["args01"].ToString() + (dtHead.Rows[i]["args03"].ToString() == "公制" ? "℃" :
                                dtHead.Rows[i]["args03"].ToString() == "英制" ? "℉" : ""));
                        _repeater = _repeater.Replace("【压力等级】", "【压力等级】" + dtHead.Rows[i]["PressureIDDesc"].ToString());
                        _repeater = _repeater.Replace("【密封等级】", "【密封等级】" + dtHead.Rows[i]["LevelType"].ToString() + " " + dtHead.Rows[i]["Level"].ToString());
                        _repeater = _repeater.Replace("【设计标准】", "【设计标准】" + dtHead.Rows[i]["args04Desc"].ToString());
                        _repeater = _repeater.Replace("【结构标准】", "【结构标准】" + dtHead.Rows[i]["args06Desc"].ToString());
                        _repeater = _repeater.Replace("【连接方式】", "【连接方式】" + dtHead.Rows[i]["ConnDesc"].ToString());
                        _repeater = _repeater.Replace("【法兰标准】", "【法兰标准】" + dtHead.Rows[i]["args05Desc"].ToString());

                        DataView dv = dtList.DefaultView;
                        dv.RowFilter = "Oid='" + Oid + "'";
                        DataTable _dtList = dv.ToTable();//根据头部筛选
                        _repeater = _repeater.Replace("rowspan=\"4\"", "rowspan=\"" + (3 + _dtList.Rows.Count) + "\"");
                        for (int j = 0; j < _dtList.Rows.Count; j++)
                        {
                            string _subRepeater = subRepeater;
                            _subRepeater = _subRepeater.Replace("【子序号】", (j + 1).ToString());
                            _subRepeater = _subRepeater.Replace("【口径】", _dtList.Rows[j]["SIDDesc"].ToString());
                            _subRepeater = _subRepeater.Replace("【数量】", _dtList.Rows[j]["Buy_num"].ToString());
                            _subRepeater = _subRepeater.Replace("【单价】", _dtList.Rows[j]["finla_price"].ToString());
                            decimal _price = decimal.Parse(_dtList.Rows[j]["Buy_num"].ToString()) *
                                decimal.Parse(_dtList.Rows[j]["finla_price"].ToString());
                            _subRepeater = _subRepeater.Replace("【总金额】", _price.ToString());
                            ToTalPrice += _price;
                            _subContent += _subRepeater;
                        }
                        _repeater = _repeater.Replace(subRepeater, _subContent);


                        finalContent += _repeater;
                    }
                    StrContact = StrContact.Replace(repeater, finalContent);
                }
                StrContact = StrContact.Replace(repeater, finalContent).Replace("【总价】", ToTalPrice.ToString());
                //插入合同表

                string insert = AddContact(strContactId, RequestID, "", StrContact, "1", seqno);

                al.Add(insert);
                //更改询价单状态
                sql = new StringBuilder();
                sql.Append("update t_request set rowstatus='3' where requestID='" + RequestID + "'");

                al.Add(sql.ToString());

                //判断是否需要预付款
                if (da.ExecuteArrayList(al))
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
            catch (Exception ex)
            {
                Log.Debug("合同生成失败", ex.Message.ToString());
                return "-1";//操作失败
            }
            return "1";//操作成功
        }
        /// <summary>
        /// 生成英文合同
        /// </summary>
        /// <param name="strContactId"></param>
        /// <param name="RequestID"></param>
        /// <param name="CusContactFilePath"></param>
        /// <returns></returns>
        public string GenerateCustomerContactEN(string strContactId, string RequestID, string CusContactFilePath)
        {
            /*
             * 表头参数列表：
             * 【客户名称】
             * 【总价】
             * 【合同编号】
             * 
             * 表体参数列表：
             * 第一层：
             * 【序号】
             * 【产品名称】
             * 【阀体】
             * 【阀芯】
             * 【阀座】
             * 【阀杆】
             * 【填料】
             * 【温度】
             * 【压力等级】
             * 【密封等级】
             * 【设计标准】
             * 【结构标准】
             * 【连接方式】
             * 【法兰标准】
             * 
             * 第二层：
             * 【子序号】
             * 【口径】
             * 【数量】
             * 【单价】
             * 【总金额】
             */
            string CusName = "";
            decimal ToTalPrice = 0;
            ArrayList al = new ArrayList();
            try
            {
                //抓取内容数据
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat(@"select au.tauName from T_Request R 
                                    left join T_AppUser au on au.tauLoginId=R.tauloginid
                                    where R.RequestID='{0}'", RequestID);
                DataTable dtHead = da.ExecuteDataTable(sql.ToString());
                if (dtHead != null && dtHead.Rows.Count > 0)
                {
                    CusName = dtHead.Rows[0]["tauName"].ToString();
                }
                DataTable dtList = new DataTable();
                GetReqDataEN(RequestID, ref dtHead, ref dtList);
                string seqno = CH.GetSequence("1");//合同号
                //读取、拼装合同
                StreamReader sr = new StreamReader(CusContactFilePath, Encoding.UTF8);
                string StrContact = sr.ReadToEnd();
                StrContact = StrContact.Replace("【客户名称】", CusName);
                StrContact = StrContact.Replace("【合同编号】", seqno);
                int _startIndex = StrContact.IndexOf("<!--产品头部开始-->") + 13;
                int _endIndex = StrContact.IndexOf("<!--产品头部结束-->");
                string repeater = StrContact.Substring(_startIndex, _endIndex - _startIndex);
                _startIndex = repeater.IndexOf("<!--子项开始-->") + 11;
                _endIndex = repeater.IndexOf("<!--子项结束-->");
                string subRepeater = repeater.Substring(_startIndex, _endIndex - _startIndex);
                string finalContent = "";
                if (dtHead.Rows.Count > 0)
                {
                    for (int i = 0; i < dtHead.Rows.Count; i++)
                    {
                        string _repeater = repeater;
                        string _subContent = "";
                        string Oid = dtHead.Rows[i]["Oid"].ToString();
                        _repeater = _repeater.Replace("【序号】", (i + 1).ToString());

                        string productionNameCH = getProductName(dtHead.Rows[i]["ProductionID"].ToString());

                        _repeater = _repeater.Replace("【产品名称】", productionNameCH + " " + dtHead.Rows[i]["ProductionDesc"].ToString());
                        _repeater = _repeater.Replace("【阀体】", "【阀体 Body】" + dtHead.Rows[i]["MID_BDesc"].ToString());
                        _repeater = _repeater.Replace("【阀芯】", "【阀芯 Trim】" + dtHead.Rows[i]["MID_IDesc"].ToString());
                        _repeater = _repeater.Replace("【阀座】", "【阀座 Seat】" + dtHead.Rows[i]["args02Desc"].ToString());
                        _repeater = _repeater.Replace("【阀杆】", "【阀杆 Stem】" + dtHead.Rows[i]["MID_SDesc"].ToString());
                        _repeater = _repeater.Replace("【填料】", "【填料 Packing】" + dtHead.Rows[i]["args09Desc"].ToString());
                        _repeater = _repeater.Replace("【温度】", "【温度 Design Temp】" + dtHead.Rows[i]["args01"].ToString() + (dtHead.Rows[i]["args03"].ToString() == "公制" ? "℃" :
                                dtHead.Rows[i]["args03"].ToString() == "英制" ? "℉" : ""));
                        _repeater = _repeater.Replace("【压力等级】", "【压力等级 Rating】" + dtHead.Rows[i]["PressureIDDesc"].ToString());
                        _repeater = _repeater.Replace("【密封等级】", "【密封等级 Leak. Class】" + dtHead.Rows[i]["LevelType"].ToString() + " " + dtHead.Rows[i]["Level"].ToString());
                        _repeater = _repeater.Replace("【设计标准】", "【设计标准 Design STD】" + dtHead.Rows[i]["args04Desc"].ToString());
                        _repeater = _repeater.Replace("【结构标准】", "【结构标准 Length STD】" + dtHead.Rows[i]["args06Desc"].ToString());
                        _repeater = _repeater.Replace("【连接方式】", "【连接方式 Flange】" + dtHead.Rows[i]["ConnDesc"].ToString());
                        _repeater = _repeater.Replace("【法兰标准】", "【法兰标准 Flange STD】" + dtHead.Rows[i]["args05Desc"].ToString());

                        DataView dv = dtList.DefaultView;
                        dv.RowFilter = "Oid='" + Oid + "'";
                        DataTable _dtList = dv.ToTable();//根据头部筛选
                        _repeater = _repeater.Replace("rowspan=\"4\"", "rowspan=\"" + (3 + _dtList.Rows.Count) + "\"");
                        for (int j = 0; j < _dtList.Rows.Count; j++)
                        {
                            string _subRepeater = subRepeater;
                            _subRepeater = _subRepeater.Replace("【子序号】", (j + 1).ToString());
                            _subRepeater = _subRepeater.Replace("【口径】", _dtList.Rows[j]["SIDDesc"].ToString());
                            _subRepeater = _subRepeater.Replace("【数量】", _dtList.Rows[j]["Buy_num"].ToString());
                            _subRepeater = _subRepeater.Replace("【单价】", _dtList.Rows[j]["finla_price"].ToString());
                            decimal _price = decimal.Parse(_dtList.Rows[j]["Buy_num"].ToString()) *
                                decimal.Parse(_dtList.Rows[j]["finla_price"].ToString());
                            _subRepeater = _subRepeater.Replace("【总金额】", _price.ToString());
                            ToTalPrice += _price;
                            _subContent += _subRepeater;
                        }
                        _repeater = _repeater.Replace(subRepeater, _subContent);


                        finalContent += _repeater;
                    }
                    StrContact = StrContact.Replace(repeater, finalContent);
                }
                StrContact = StrContact.Replace(repeater, finalContent).Replace("【总价】", ToTalPrice.ToString());
                //插入合同表

                string insert = AddContact(strContactId, RequestID, "", StrContact, "1", seqno);

                al.Add(insert);
                //更改询价单状态
                sql = new StringBuilder();
                sql.Append("update t_request set rowstatus='3' where requestID='" + RequestID + "'");

                al.Add(sql.ToString());

                //判断是否需要预付款
                if (da.ExecuteArrayList(al))
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
            catch (Exception ex)
            {
                Log.Debug("合同生成失败", ex.Message.ToString());
                return "-1";//操作失败
            }
            return "1";//操作成功
        }
        /// <summary>
        /// 生成供应商合同
        /// </summary>
        /// <param name="RequestID">询价表头ID</param>
        public string GenerateSupplyContact(string RequestID, string SupContactFilePath)
        {

            /* 表头参数：
             * 【供货单位名称】
             * 【总计】
             * 【订单编号】
             * 
             * 表体参数列表：
             * 第一层：
             * 【序号】
             * 【产品名称】
             * 【阀体】
             * 【阀芯】
             * 【阀座】
             * 【阀杆】
             * 【填料】
             * 【温度】
             * 【压力等级】
             * 【密封等级】
             * 【设计标准】
             * 【结构标准】
             * 【连接方式】
             * 【法兰标准】
             * 
             * 第二层：
             * 【子序号】
             * 【口径】
             * 【数量】
             * 【单价】
             * 【总金额】
             */
            try
            {
                //分供应商抓取内容数据
                StringBuilder sql = new StringBuilder();
                ArrayList al = new ArrayList();
                sql.AppendFormat(@"select distinct au.tauName,au.tauloginID,r.ResponseID from T_Response r
                                        left join T_AppUser au on au.tauLoginId=r.tauloginid 
                                        left join T_ResponseDetail rd on rd.ResponseID=r.ResponseID
                                        where rd.Is_Confirmed='1' and r.RequestID='{0}'", RequestID);

                DataTable dtHead = da.ExecuteDataTable(sql.ToString());
                if (dtHead != null && dtHead.Rows.Count > 0)
                {//按供应商汇总报价
                    for (int ii = 0; ii < dtHead.Rows.Count; ii++)
                    {
                        string SupName = dtHead.Rows[ii]["tauName"].ToString();
                        string tauloginID = dtHead.Rows[ii]["tauLoginID"].ToString();
                        string ResponseID = dtHead.Rows[ii]["ResponseID"].ToString();
                        decimal TotalPrice = 0;

                        DataTable dtHeader = new DataTable();
                        DataTable dtList = new DataTable();
                        GetResData(ResponseID, ref dtHeader, ref dtList);
                        DataView dvList = dtList.DefaultView;
                        dvList.RowFilter = "Is_Confirmed='1'";
                        dtList = dvList.ToTable();
                        dtHeader.Columns.Add("selected");
                        for (int mi = 0; mi < dtList.Rows.Count; mi++)
                        {
                            for (int mj = 0; mj < dtHeader.Rows.Count; mj++)
                            {
                                if (dtHeader.Rows[mj]["Oid"].ToString() == dtList.Rows[mi]["Oid"].ToString())
                                {
                                    dtHeader.Rows[mj]["selected"] = "true";
                                }
                            }
                        }
                        DataView dtHeaderView = dtHeader.DefaultView;
                        dtHeaderView.RowFilter = "selected='true'";
                        dtHeader = dtHeaderView.ToTable();
                        //读取、拼装合同
                        StreamReader sr = new StreamReader(SupContactFilePath, Encoding.UTF8);
                        string StrContart = sr.ReadToEnd();
                        sr.Dispose();
                        StrContart = StrContart.Replace("【供货单位名称】", SupName);
                        Log.Debug("GenerateSupplyContact-4", StrContart);
                        int _startIndex = StrContart.IndexOf("<!--产品头部开始-->") + 13;
                        int _endIndex = StrContart.IndexOf("<!--产品头部结束-->");
                        string repeater = StrContart.Substring(_startIndex, _endIndex - _startIndex);
                        _startIndex = repeater.IndexOf("<!--子项开始-->") + 11;
                        _endIndex = repeater.IndexOf("<!--子项结束-->");
                        string subRepeater = repeater.Substring(_startIndex, _endIndex - _startIndex);
                        string finalContent = "";
                        string seqno = CH.GetSequence("2");//获取合同编号
                        string seqHeader = seqno.Substring(0, 7);
                        string strContactId =new bus_C_Conn().getNewId();
                        int _count = int.Parse(seqno.Substring(7));
                        string _totalSeq = seqHeader + (_count + ii).ToString().PadLeft(4, '0');//reunion
                        StrContart = StrContart.Replace("【订单编号】", _totalSeq);

                        if (dtHeader.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtHeader.Rows.Count; i++)
                            {
                                string _repeater = repeater;
                                string _subContent = "";
                                string Oid = dtHeader.Rows[i]["Oid"].ToString();
                                _repeater = _repeater.Replace("【序号】", (i + 1).ToString());
                                _repeater = _repeater.Replace("【产品名称】", dtHeader.Rows[i]["ProductionDesc"].ToString());
                                _repeater = _repeater.Replace("【阀体】", "【阀体】" + dtHeader.Rows[i]["MID_BDesc"].ToString());
                                _repeater = _repeater.Replace("【阀芯】", "【阀芯】" + dtHeader.Rows[i]["MID_IDesc"].ToString());
                                _repeater = _repeater.Replace("【阀座】", "【阀座】" + dtHeader.Rows[i]["args02Desc"].ToString());
                                _repeater = _repeater.Replace("【阀杆】", "【阀杆】" + dtHeader.Rows[i]["MID_SDesc"].ToString());
                                _repeater = _repeater.Replace("【填料】", "【填料】" + dtHeader.Rows[i]["args09Desc"].ToString());
                                _repeater = _repeater.Replace("【温度】", "【温度】" + dtHeader.Rows[i]["args01"].ToString() + (dtHeader.Rows[i]["args03"].ToString() == "公制" ? "℃" :
                                dtHeader.Rows[i]["args03"].ToString() == "英制" ? "℉" : ""));
                                _repeater = _repeater.Replace("【压力等级】", "【压力等级】" + dtHeader.Rows[i]["PressureIDDesc"].ToString());
                                _repeater = _repeater.Replace("【密封等级】", "【密封等级】" + dtHeader.Rows[i]["LevelType"].ToString() + " " + dtHeader.Rows[i]["Level"].ToString());
                                _repeater = _repeater.Replace("【设计标准】", "【设计标准】" + dtHeader.Rows[i]["args04Desc"].ToString());
                                _repeater = _repeater.Replace("【结构标准】", "【结构标准】" + dtHeader.Rows[i]["args06Desc"].ToString());
                                _repeater = _repeater.Replace("【连接方式】", "【连接方式】" + dtHeader.Rows[i]["ConnDesc"].ToString());
                                _repeater = _repeater.Replace("【法兰标准】", "【法兰标准】" + dtHeader.Rows[i]["args05Desc"].ToString());

                                DataView dv = dtList.DefaultView;
                                dv.RowFilter = "Oid='" + Oid + "' and Is_Confirmed='1'";
                                DataTable _dtList = dv.ToTable();//根据头部筛选
                                _repeater = _repeater.Replace("rowspan=\"4\"", "rowspan=\"" + (3 + _dtList.Rows.Count) + "\"");
                                for (int j = 0; j < _dtList.Rows.Count; j++)
                                {
                                    string _subRepeater = subRepeater;
                                    _subRepeater = _subRepeater.Replace("【子序号】", (j + 1).ToString());
                                    _subRepeater = _subRepeater.Replace("【口径】", _dtList.Rows[j]["SIDDesc"].ToString());
                                    _subRepeater = _subRepeater.Replace("【数量】", _dtList.Rows[j]["Buy_num"].ToString());
                                    _subRepeater = _subRepeater.Replace("【交期】", _dtList.Rows[j]["deliver_time"].ToString());
                                    _subRepeater = _subRepeater.Replace("【单价】", _dtList.Rows[j]["single_price"].ToString());
                                    decimal _price = decimal.Parse(_dtList.Rows[j]["Buy_num"].ToString()) *
                                        decimal.Parse(_dtList.Rows[j]["single_price"].ToString());
                                    _subRepeater = _subRepeater.Replace("【总金额】", _price.ToString());
                                    TotalPrice += _price;
                                    _subContent += _subRepeater;
                                }
                                _repeater = _repeater.Replace(subRepeater, _subContent);


                                finalContent += _repeater;
                            }
                        }

                        StrContart = StrContart.Replace(repeater, finalContent).Replace("【总价】", TotalPrice.ToString());
                        //插入

                        string insert = AddContact(strContactId, RequestID, ResponseID, StrContart, "2", _totalSeq);

                        al.Add(insert);
                        //更新表头状态
                        sql = new StringBuilder();
                        sql.Append("update T_Response set rowstatus='3' where ResponseID='" + ResponseID + "'");

                        al.Add(sql.ToString());
                    }
                    Log.Debug("供应商合同生成结果", da.ExecuteArrayList(al).ToString());
                }

            }
            catch (Exception ex)
            {
                Log.Debug("供应商合同生成结果", ex.Message.ToString());
                return "-1";
            }
            return "1";
        }
        /// <summary>
        /// 新增合同
        /// </summary>
        /// <param name="requestID"></param>
        /// <param name="response"></param>
        /// <param name="content">内容</param>
        /// <param name="type">1客户 2供应商</param>
        /// <returns></returns>
        public string AddContact(string ContractID, string requestID, string response, string content, string type)
        {
            StringBuilder sql = new StringBuilder();
            content = content.Replace("'", "\"");
            sql.AppendFormat("insert into T_Contract (ContractID,RequestID,ResponseID,[content],type)values('{0}','{1}','{2}','{3}','{4}')",
                ContractID, requestID, response, content, type);
            return sql.ToString();
        }
        /// <summary>
        /// 新增合同
        /// </summary>
        /// <param name="ContractID"></param>
        /// <param name="requestID"></param>
        /// <param name="response"></param>
        /// <param name="content">内容</param>
        /// <param name="type">1客户 2供应商</param>
        /// <param name="seqno">合同序列号</param>
        /// <returns></returns>
        public string AddContact(string ContractID, string requestID, string response, string content, string type, string seqno)
        {
            StringBuilder sql = new StringBuilder();
            content = content.Replace("'", "\"");
            //新增pdf生成和插入
            string filename = "";
            if (!CH.GeneretePdf(content, out filename))
            {
                throw new Exception("pdf generete failed");
            }
            sql.AppendFormat("insert into T_Contract (ContractID,RequestID,ResponseID,[content],type,seqno,PdfFilePath)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                ContractID, requestID, response, content, type, seqno, filename);
            return sql.ToString();
        }

        private void GetReqData(string strRequestId, ref DataTable dtHead, ref DataTable dtList)
        {
            string strUrl = ConfigurationManager.AppSettings["UploadFilePath"];
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", "zh");
            parm.Add("@RequestId", strRequestId);
            parm.Add("@strUrl", strUrl);
            //DataTable dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetailHead", parm);
            dtHead = new bus_T_Request().getDataTable("SP_SelectRequestDetailHead", parm);

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", "zh");
            parm1.Add("@RequestId", strRequestId);
            parm1.Add("@strUrl", strUrl);
            //DataTable dtList = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetail", parm1);
            dtList = new bus_T_Request().getDataTable("SP_SelectRequestDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion
        }
        private void GetReqDataEN(string strRequestId, ref DataTable dtHead, ref DataTable dtList)
        {
            string strUrl = ConfigurationManager.AppSettings["UploadFilePath"];
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", "en");
            parm.Add("@RequestId", strRequestId);
            parm.Add("@strUrl", strUrl);
            //dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetailHead", parm);
            dtHead = new bus_T_Request().getDataTable("SP_SelectRequestDetailHead", parm);

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", "en");
            parm1.Add("@RequestId", strRequestId);
            parm1.Add("@strUrl", strUrl);
            //dtList = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetail", parm1);
            dtList = new bus_T_Request().getDataTable("SP_SelectRequestDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion
        }
        private void GetResData(string strResponseID, ref DataTable dtHead, ref DataTable dtList)
        {
            string strUrl = ConfigurationManager.AppSettings["UploadFilePath"];
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", "zh");
            parm.Add("@ResponseId", strResponseID);
            parm.Add("@strUrl", strUrl);
            //dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetailHead", parm);
            dtHead = new bus_T_Request().getDataTable("SP_SelectResponseDetailHead", parm);

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", "zh");
            parm1.Add("@ResponseId", strResponseID);
            parm1.Add("@strUrl", strUrl);
            //dtList = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetail", parm1);
            dtList = new bus_T_Request().getDataTable("SP_SelectResponseDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (dtList.Rows[i]["ResponseID"].ToString() == dtHead.Rows[j]["ResponseID"].ToString() &&
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion
        }
        //获取产品中文名称
        private string getProductName(string productionID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select [desc] from T_Production_MultiLan where ProductionID='{0}' and LanId='zh'", productionID);
            object o = da.ExecuteScalar(sql.ToString());
            if (o != null && o.ToString() != "")
            {
                return o.ToString();
            }
            return "";
        }
    }
}